using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Security.Cryptography;
using PlugNT.Safe;
using PlugNT.Database;
using PlugNT.Database.Common.Simple;
using PlugNT.Cms.Custom;
using PlugNT.Cms.Model;

namespace PlugNT.Cms.DAL
{
    public class Feedback
    {

        public static string currTableName = WebConfig.TablePrefix + "feedback";





 






        public FeedbackInfo GetFeedbackInfo(int id)
        {
            FeedbackInfo model = null;
            IDataReader dr = GetReader(1, "*", " and id=" + id);
            if (dr != null)
            {
                if (dr.Read())
                {
                    model = new FeedbackInfo();
                    model.id = int.Parse(dr["id"].ToString().ToString());
                    model.sys_model_id = int.Parse(dr["sys_model_id"].ToString());
                    model.username = dr["username"].ToString();
                    model.ip = dr["ip"].ToString();
                    model.locked = dr["locked"].ToString() == "1";
                    model.title = dr["title"].ToString();
                    model.content = dr["content"].ToString();
                    model.reply = dr["reply"].ToString();
                    model.addtime = DateTime.Parse(dr["addtime"].ToString());
                    model.contacts = dr["contacts"].ToString();
                }
                dr.Close();
            }
            return model;
        }










        public bool Add(FeedbackInfo model)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into " + currTableName + "(");
            strSql.Append("sys_model_id,[username],ip,locked,title,content,reply,addtime,contacts");
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(model.sys_model_id.ToString() + ",");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.username) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.ip) + "',");
            strSql.Append((model.locked ? "1" : "0") + ",");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.title) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.content) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.reply) + "',");
            strSql.Append(WebConfig.IsAccess ? "Now()," : "getdate(),");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.contacts) + "'");
            strSql.Append(")");

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }






        public bool Update(FeedbackInfo model)
        {

            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + currTableName + " set ");
            strSql.Append("content='" + PlugNT.Safe.StringHelper.SqlFilter(model.content) + "',");
            strSql.Append("reply='" + PlugNT.Safe.StringHelper.SqlFilter(model.reply) + "'");
            strSql.Append(" where id=" + model.id.ToString());

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }


        public bool UpdateLocked(int id, bool locked)
        {

            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + currTableName + " set ");
            strSql.Append("locked=" + (locked ? "1" : "0"));
            strSql.Append(" where id=" + id.ToString());

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }









        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from " + currTableName);
            strSql.Append(" where id=" + id.ToString());
            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }













        private IDataReader GetReader(int topCount, string colNames, string andWhere)
        {
            string sql = "select " + (topCount == -1 ? "" : "top " + topCount) + " " + colNames + " from " + currTableName + " where 1=1 " + andWhere;
            return DbHelper.ExecuteReader(sql);
        }






        public DataTable GetList(int sysModelId, string andWhere)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from  [" + currTableName + "] where sys_model_id= " + sysModelId);
            strSql.Append(andWhere);
            return DbHelper.GetDataTable(strSql.ToString());
        }



        public DataTable GetList(int sysModelId, int pageSize, int currPage, string andWhere)
        {

            string sql = Sql.GetRePagingString(pageSize, currPage, "[" + currTableName + "]", "id"," and sys_model_id= " + sysModelId + andWhere);
            return DbHelper.GetDataTable(sql);
        }

        public DataTable GetList(int sysModelId, int pageSize, int currPage, string columns, string andWhere, LeftJoinInfo joininfo)
        {
            string sql = Sql.GetRePagingString(pageSize, currPage, currTableName, "id", columns, " and sys_model_id=" + sysModelId + andWhere, joininfo);
            return DbHelper.GetDataTable(sql);
        }




        public int GetCount(int sysModelId, string andWhere)
        {
            string sql = "select count(1) from [" + currTableName + "] where sys_model_id=" + sysModelId + andWhere;
            return (int)DbHelper.GetSingle(sql);
        }



    }
}

